from django.contrib.auth import authenticate, login, logout
from django.core.paginator import Paginator
from django.db.models import Q
from django.shortcuts import render,HttpResponse,redirect
from .models import Doctor,Recipe
from django.contrib.auth.decorators import login_required

# Create your views here.

def index(request):
    return render(request,'index.html')


def register(request):
    if request.method == 'GET':
        return render(request, 'register.html')
    if request.method == 'POST':
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        password2 = request.POST.get('password2', '')
        if password == password2:
            a = Recipe.objects.filter(username=username)
            if a:
                return render(request, 'register.html', {'error': '用户名存在，请您重新输入'})
            else:
                Recipe.objects.create_user(username=username, password=password)
                return redirect('app1:loginx')
        else:
            return render(request, 'register.html', {'error': '密码不一致，请您重新输入'})


@login_required
def loginx(request):
    if request.method == 'GET':
        return render(request, 'loginx.html')
    if request.method == 'POST':
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        recipe = authenticate(username=username, password=password)
        if recipe:
            login(request,user=recipe)
            return redirect('app1:recipes')
        else:
            return render(request, 'loginx.html', {'er': '账号或者密码错误'})


def recipe_add(request):
    doctors=Doctor.objects.all()
    if request.method=='GET':
        return render(request,'recipe_add.html',{'doctors':doctors})
    if request.method=='POST':
        cname=request.POST.get('cname','')
        result=request.POST.get('result','')
        hname=request.POST.get('hname','')
        doctor=request.POST.get('doctor','')

        Doctor.objects.create(
            cname=cname,
            result=result,
            hname=hname,
            doctor=doctor,
        )
        return redirect('app1:recipes')

def recipe_update(request,id):
    doctor = Doctor.objects.all()
    recipe = Recipe.objects.filter(id=id)
    if request.method == 'GET':
        return render(request, 'recipe_update.html', {'recipe': recipe, 'doctor': doctor})
    if request.method == 'POST':
        cname = request.POST.get('cname', '')
        result = request.POST.get('result', '')
        doctor = request.POST.get('doctor', '')
        hname = request.POST.get('hname', '')

        Recipe.objects.filter(id=id).update(
            cname=cname,
            result=result,
            doctor=doctor,
            hname=hname,
        )
        return redirect('app1:recipes')


def recipe_del(request,recipeid):
    recipe = Recipe.objects.get(id=recipeid)
    recipe.delete()
    return redirect('app1:recipes')


def recipes(request):
    wd=request.GET.get('wd','')
    if wd:
        recipe=Recipe.objects.filter(Q(hname__icontains=wd) | Q(cname__icontains=wd)).all()
    else:
        recipe=Recipe.objects.all()
    page=request.GET.get('page',1)
    page=int(page)

    paginator=3
    recipe1=Paginator(recipe,paginator)
    a=recipe1.page(page)
    recipe=a.object_list
    max=recipe1.num_pages

    return render(request,'recipes.html',{
        'page':page,
        'recipe':recipe,
        'max':max,
                                          })
#加了个注销
def logoutx(request):
    logout(request)
    return redirect('app1:loginx')


